Setup
mkdir app-name
cd myapp
npm init
entry point: (index.js)
npm install express
npm install express --no-save
In index.js
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
Run app
node app.js
Express application generator
npx express-generator
or
npm install -g express-generator
express
cd myapp
npm install
Basic routing
app.METHOD(PATH, HANDLER)
The following examples illustrate defining simple routes.
Respond with Hello World!
on the homepage:
app.get('/', (req, res) => {
res.send('Hello World!')
})
Respond to POST request on the root route (/
), the application’s home page:
app.post('/', (req, res) => {
res.send('Got a POST request')
})
Respond to a PUT request to the /user
route:
app.put('/user', (req, res) => {
res.send('Got a PUT request at /user')
})
Respond to a DELETE request to the /user
route:
app.delete('/user', (req, res) => {
res.send('Got a DELETE request at /user')
})
Serving static files in Express
express.static(root, [options])
app.use(express.static('public'))
// To use multiple static assets directories, call the express.static middleware function multiple times:
app.use(express.static('public'))
app.use(express.static('files'))
app.use('/static', express.static('public'))
const path = require('path')
app.use('/static', express.static(path.join(__dirname, 'public')))